// base
import Taro, { Component } from '@tarojs/taro'
import { View, Text, Image } from '@tarojs/components'
import { connect } from '@tarojs/redux'

// less
import './index.less'
import { showToast } from '../../../../crossplatform/apiservice/toast'
import { navigateTo } from '../../../../crossplatform/apiservice/navigate'
import pagejumplist from '../../../../configuration/pagejumplist.json'
import { getZhichuBUserInfo } from '../../../../store/actions/userinfo'
import { UPDATEUSERINFO } from '../../../../store/types/userinfo'
import { UPDATEPAGESELFCENTER } from '../../store/types/pageselfcenter'
import scaleImageSrc from '../../../../util/pagemethods/scaleImageSrc'
import { avaterB } from '../../../../configuration/staticimg'
import tool from '../../../../util/tool'

@connect(
  ({ userinfo }) => ({
    ...userinfo
  }),
  dispatch => ({
    getZhichuBUserInfo (payload) {
      return new Promise(resolve => {
        let promise = getZhichuBUserInfo(payload)
        dispatch(promise)
        promise.payload.then(data => {
          resolve(data)
        })
      })
    },
    updateSelfCenter (payload) {
      dispatch({
        type: UPDATEPAGESELFCENTER,
        payload: {
          ...payload
        }
      })
    },
    updateState (payload) {
      dispatch({
        type: UPDATEUSERINFO,
        payload: {
          ...payload
        }
      })
    }
  })
)
class CompanyMessage extends Component {
  componentWillMount () {
    // onLoad
  }

  componentDidMount () {
    // onready
  }

  componentWillUnmount () {
    // onUnload
  }

  componentWillReceiveProps (nextprops) {}

  gotocpmessage = () => {
    navigateTo({
      url: pagejumplist['company-represent-infor'].path
    })
  }

  goLogin = () => {
    this.props.onGoLogin({ action: 'click' })
  }

  delay (time = 0) {
    return new Promise(resolve => {
      setTimeout(() => {
        resolve()
      }, time)
    })
  }

  gotoPeopleCollection = tool.throttle(() => {
    const check = this.doCheckUser()
    if (check) {
      navigateTo({
        url: pagejumplist['people-collection'].path
      })
    } else {
      showToast({
        title: '请先登录！'
      })
    }
  })

  gotoJobManage = tool.throttle(() => {
    const { postException } = this.props
    const check = this.doCheckUser()
    if (check) {
      navigateTo({
        url: `${pagejumplist['manage-jobs'].path}?postException=${postException}`
      })
    } else {
      showToast({
        title: '请先登录！'
      })
    }
  })

  gotoTalentCommunicate = tool.throttle(() => {
    const { ZtalentTalk } = this.props
    const check = this.doCheckUser()
    if (check) {
      navigateTo({
        url: `${pagejumplist['talent-communicate'].path}?total=${ZtalentTalk}`
      })
    } else {
      showToast({
        title: '请先登录！'
      })
    }
  })

  doCheckUser = () => {
    if (this.props.zhichuUserIdB) {
      return true
    } else {
      return false
    }
  }

  noOpen = () => {
    showToast({
      title: '功能暂未开启'
    })
  }

  gotoSetting = tool.throttle(() => {
    navigateTo({
      url: pagejumplist['setting-page'].path
    })
  })

  goUploadAvator = () => {
    this.props.onUploadAvator()
  }

  render () {
    const { userInfoB, ZcollectCount, ZmanageCount, ZtalentTalk, postException } = this.props
    return (
      <View className="CompanyMessage_id11447">
        <View className="mymessagewrap">
          <View className="userAvator-wrap">
            <Image
              className="mymessageright"
              src={scaleImageSrc(userInfoB.userAvator || avaterB(userInfoB.sex))}
              mode="aspectFill"
              onClick={this.goUploadAvator}
              lazy-load
            />
            <View className="name-right" onClick={this.goLogin}>
              <View className="name">{userInfoB.userName || '登录'}</View>
              <View className="subtitle">{userInfoB.position || ''}</View>
            </View>
          </View>
          <View className="setting" onClick={this.gotoSetting}>
            <Image
              className="setting-img"
              src="https://imagesize.hrwork.com/sp/files/1aea35da-efe1-4ab0-b3ee-ec5517534ae9.png"
              lazy-load
            />
          </View>
        </View>

        <View className="messagelist">
          <View className="mslist" onClick={this.gotoPeopleCollection}>
            <View className="text">人才收藏</View>
            <View className="num">{ZcollectCount || 0}</View>
            <Image
              className="icon"
              src="https://imagesize.hrwork.com/sp/files/059380d3-a914-489c-9e36-89a298ce9fa2.png"
            />
          </View>
          <View className="mslist" onClick={this.gotoTalentCommunicate}>
            <View className="text">沟通历史</View>
            <View className="num">{ZtalentTalk || 0}</View>
            <Image
              className="icon"
              src="https://imagesize.hrwork.com/sp/files/2bf48289-d90a-4335-98cb-512ab39cfafb.png"
            />
          </View>
        </View>
      </View>
    )
  }
}

export default CompanyMessage
